package com.scan.bus.util;

import android.os.Handler;
import android.os.Message;

import java.util.Timer;
import java.util.TimerTask;

public class TimerTaskUtil {

    private static TimerTaskUtil timerUtil = new TimerTaskUtil();
    private TimerTask timerTask;
    private ITimerListener listener;
    private Timer timer;
    private long seconds = 0;
    private final int callbackWhat = 200;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == callbackWhat){
                MsgBody obj = (MsgBody) msg.obj;
                if (listener != null){
                    listener.time(obj.time,obj.seconds);
                }
                obj = null;
            }
        }
    };

    public static TimerTaskUtil getInstance(){
        return timerUtil;
    }

    private TimerTaskUtil(){
        timer = new Timer();

        if (timerTask == null)
            timerTask = getTimerTask();
    }

    private TimerTask getTimerTask(){
        timerTask = new TimerTask() {
            @Override
            public void run() {
                seconds += 1;
                String time = formatSeconds(seconds);
                Message obtain = Message.obtain();
                obtain.what = callbackWhat;
                obtain.obj = new MsgBody(seconds,time);
                mHandler.sendMessage(obtain);
            }
        };
        return timerTask;
    }

    public void start(){
        if (timer == null)
            timer = new Timer();

        if (timerTask == null)
            timerTask = getTimerTask();

        timer.schedule(timerTask,0,1000);

    }

    /**
     * 设置开始时长
     * @param seconds
     */
    public void setStartTime(long seconds){
        this.seconds = seconds;
    }

    public void stop(){
        if (timer != null)
            timer.cancel();
        if (timerTask != null)
            timerTask.cancel();

        if (mHandler != null){
            mHandler.removeCallbacksAndMessages(null);
        }
        timerTask = null;
        timer = null;
        listener = null;
    }

    public void cancle(){
        if (timer != null)
            timer.cancel();
        if (timerTask != null)
            timerTask.cancel();

        if (mHandler != null){
            mHandler.removeCallbacksAndMessages(null);
        }
        timerTask = null;
        timer = null;
        seconds = 0;
        listener = null;
    }

    public void destory(){
        cancle();
        timerTask = null;
        timer = null;
        seconds = 0;
        listener = null;
    }

    private String formatSeconds(long seconds){
        String standardTime;
        standardTime = String.format("%02d:%02d:%02d", seconds / 3600, seconds % 3600 / 60, seconds % 60);
        return standardTime;
    }

    public void setTimerListener(ITimerListener listener){
        this.listener = listener;
    }


    public interface ITimerListener{
        void time(String time,long seconds);
    }

    class MsgBody{
        private long seconds;
        private String time;

        public MsgBody(long seconds,String time){
            this.seconds = seconds;
            this.time = time;
        }
    }
}
